package org.yinweichen.llm_dataset_backend.repository;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.yinweichen.llm_dataset_backend.entity.StandardQuestion;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
import org.yinweichen.llm_dataset_backend.entity.StandardQuestionId;

import java.util.List;
import java.util.Set;

@Repository
public interface StandardQuestionRepository extends JpaRepository<StandardQuestion, StandardQuestionId>, JpaSpecificationExecutor<StandardQuestion> {

    @Modifying
    @Query("UPDATE StandardQuestion q SET q.active = false WHERE q.id = :id AND q.version = :version")
    void inactivate(@Param("id") Long id, @Param("version") String version);

    @Modifying
    @Query("UPDATE StandardQuestion q SET q.active = true WHERE q.id = :id AND q.version = :version")
    void activate(@Param("id") Long id, @Param("version") String version);

    //TODO:增加version字段的约束
    @Modifying
    @Query("UPDATE StandardQuestion q SET q.question=:question, q.category=:category WHERE q.id = :id AND q.version = :version")
    void update(@Param("question") String question, @Param("category") String category, @Param("id") Long id, @Param("version") String version);

    /**
     * 查询当前版本活跃的标准问题
     * @param version 版本号
     * @param pageable 分页参数
     * @return 活跃的标准问题列表
     */
    @Query("SELECT q FROM StandardQuestion q WHERE q.active = true AND q.originalQuestion.version = :version")
    List<StandardQuestion> findActiveQuestions(
            @Param("version") String version,
            org.springframework.data.domain.Pageable pageable);

    /**
     * 查询当前版本不活跃的标准问题
     * @return 不活跃的标准问题列表
     */
    @Query("SELECT q FROM StandardQuestion q WHERE q.active = false AND q.originalQuestion.version = :version")
    List<StandardQuestion> findInactiveQuestions(
            @Param("version") String version,
            org.springframework.data.domain.Pageable pageable);

    /**
     * 查询没有对应标准答案的标准问题
     * @return 没有标准答案的标准问题列表
     */
    @Query("SELECT q FROM StandardQuestion q WHERE NOT EXISTS (SELECT 1 FROM StandardAnswer a WHERE a.questionId = q.id AND a.version = q.version) AND q.active = true AND q.originalQuestion.version = :version")
    List<StandardQuestion> findQuestionsWithoutAnswers(
            @Param("version") String version,
            org.springframework.data.domain.Pageable pageable);

    /**
     * 根据原始问题ID和版本号查找关联的标准问题
     * @param originalId 原始问题ID
     * @param version 版本号
     * @return 关联的标准问题列表
     */
    @Query("SELECT q FROM StandardQuestion q WHERE q.originalQuestion.id = :originalId AND q.originalQuestion.version = :version")
    List<StandardQuestion> findByOriginalQuestionIdAndOriginalQuestionVersion(
            @Param("originalId") Long originalId,
            @Param("version") String version);

    /**
     * 查询没有对应标准答案的活跃标准问题数量
     * @param version 版本号
     * @return 没有标准答案的活跃标准问题数量
     */
    @Query("SELECT COUNT(q) FROM StandardQuestion q WHERE NOT EXISTS (SELECT 1 FROM StandardAnswer a WHERE a.questionId = q.id AND a.version = q.version) AND q.active = true AND q.originalQuestion.version = :version")
    Long countQuestionsWithoutAnswers(@Param("version") String version);

    @Query("SELECT COUNT(q) FROM StandardQuestion q WHERE q.active = true AND q.originalQuestion.version = :version")
    Long countActiveQuestions(@Param("version") String version);

    @Query("SELECT COUNT(q) FROM StandardQuestion q WHERE q.active = false AND q.originalQuestion.version = :version")
    Long countInactiveQuestions(@Param("version") String version);

    boolean existsById(Long id);

    @Query("SELECT qt.question.id, qt.question.version FROM QuestionTag qt " +
            "WHERE qt.version = :version AND qt.tag IN :tags " +
            "GROUP BY qt.question.id, qt.question.version " +
            "HAVING COUNT(DISTINCT qt.tag) = :tagCount")
    List<Long> findQuestionsWithAllTags(
            @Param("version") String version,
            @Param("tags") Set<String> tags,
            @Param("tagCount") int tagCount);

    @Query("SELECT DISTINCT q.category FROM StandardQuestion q WHERE q.version = :version AND q.category IS NOT NULL")
    List<String> findDistinctCategoriesByVersion(@Param("version") String version);

    @Modifying
    @Query("UPDATE StandardQuestion q SET q.category = :category WHERE q.id = :id AND q.version = :version")
    void updateCategory(@Param("id") Long id, @Param("version") String version, @Param("category") String category);

    /**
     * 硬删除指定版本中所有不活跃的标准问题
     * @param version 版本号
     */
    @Modifying
    @Query("DELETE FROM StandardQuestion q WHERE q.active = false AND q.version = :version")
    void hardDelete(@Param("version") String version);
}
